#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class StudentStatusProvider : CY.GFive.Core.Providers.DALProvider.IStudentStatusProvider
	{
		#region Sql
		private static readonly string SqlSelectStudentStatus = "SELECT [Id], [StdStatus],[YearNum],[Terminal],[StdCode] FROM [StudentStatus] WHERE  [Id]=@Id";

		private static readonly string SqlInsertStudentStatus = "INSERT INTO [StudentStatus]([StdStatus],[YearNum],[Terminal],[StdCode]) values(@StdStatus,@YearNum,@Terminal,@StdCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateStudentStatus = "UPDATE [StudentStatus] SET [StdStatus] = @StdStatus,[YearNum] = @YearNum,[Terminal] = @Terminal,[StdCode] = @StdCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteStudentStatus = "DELETE [StudentStatus] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllStudentStatus = "SELECT [Id], [StdStatus],[YearNum],[Terminal],[StdCode] FROM [StudentStatus]";
		#endregion

		#region IStudentStatusProvider Members
		public Core.Business.StudentStatus Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectStudentStatus);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.StudentStatus studentStatus = new Core.Business.StudentStatus();

				if (!reader.IsDBNull(0)) studentStatus.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) studentStatus.StdStatus = reader.GetString(1);
				if (!reader.IsDBNull(2)) studentStatus.YearNum = reader.GetString(2);
				if (!reader.IsDBNull(3)) studentStatus.Terminal = reader.GetInt32(3);
				if (!reader.IsDBNull(4)) studentStatus.StdCode = reader.GetString(4);

				return studentStatus;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.StudentStatus studentStatus)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdStatus", SqlDbType.NVarChar, studentStatus.StdStatus);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, studentStatus.YearNum);
			sql.AddParameter("@Terminal", SqlDbType.Int, studentStatus.Terminal);
			sql.AddParameter("@StdCode", SqlDbType.NVarChar, studentStatus.StdCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertStudentStatus);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) studentStatus.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.StudentStatus studentStatus)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdStatus", SqlDbType.NVarChar, studentStatus.StdStatus);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, studentStatus.YearNum);
			sql.AddParameter("@Terminal", SqlDbType.Int, studentStatus.Terminal);
			sql.AddParameter("@StdCode", SqlDbType.NVarChar, studentStatus.StdCode);
			sql.AddParameter("@Id", SqlDbType.Int, studentStatus.Id);
			sql.ExecuteSql(SqlUpdateStudentStatus);
		}

		public void Delete(Core.Business.StudentStatus studentStatus)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, studentStatus.Id);
			sql.ExecuteSql(SqlDeleteStudentStatus);
		}

		public IList<Core.Business.StudentStatus> GetAllStudentStatus()
		{
			IList<Core.Business.StudentStatus> studentStatuslist = new List<Core.Business.StudentStatus>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllStudentStatus);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.StudentStatus studentStatus = new Core.Business.StudentStatus();

					if (!reader.IsDBNull(0)) studentStatus.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) studentStatus.StdStatus = reader.GetString(1);
					if (!reader.IsDBNull(2)) studentStatus.YearNum = reader.GetString(2);
					if (!reader.IsDBNull(3)) studentStatus.Terminal = reader.GetInt32(3);
					if (!reader.IsDBNull(4)) studentStatus.StdCode = reader.GetString(4);

					studentStatus.MarkOld();
					studentStatuslist.Add(studentStatus);
				}
				reader.Close();
			}
			return studentStatuslist;
		}
		#endregion
	}
}
